using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;
using System.Text;
using System.Diagnostics;


    public class BSPNode : MonoBehaviour
    {
	
       private enum Orientation { Horizontal, Vertical };

       public BSPNode(double xOffset, double yOffset, double endxpoint, double endypoint)
        {
            LeftEdge = xOffset;
            RightEdge = endxppoint;
            TopEdge = yOffset;
            BottomEdge = endypoint;

            NodeWidth = RightEdge - LeftEdge;
            NodeHeight = BottomEdge - TopEdge;
        }

      public void Split()
        {
            if (NodeWidth / NodeHeight > MsplitRatio)
                SplitOrientation = Orientation.Horizontal;
            else if (NodeHeight / NodeWidth > MSplitRatio)
                SplitOrientation = Orientation.Vertical;
            
            if (SplitOrientation == Orientation.Horizontal)
            {
               SplitLocation = LeftEdge + .5 * NodeWidth;
                Lchild = new BSPNode(LeftEdge, TopEdge, SplitLocation, BottomEdge);
                Rchild = new BSPNode(SplitLocation, TopEdge, RightEdge, BottomEdge);
               
                if (Splitit(SplitLocation - LeftEdge))
                    Lchild.Split();
                if (Splitit(RightEdge - SplitLocation))
                    Rchild.Split();
            }
            else 
            {
                SplitLocation = TopEdge + .5 * NodeHeight;

                Lchild = new BSPNode(LeftEdge, TopEdge, RightEdge, SplitLocation);
                Rchild = new BSPNode(LeftEdge, SplitLocation, RightEdge, BottomEdge);

                if (Splitit(SplitLocation - TopEdge))
                    Lchild.Split();
                if (Splitit(BottomEdge - SplitLocation))
                    Rchild.Split();
            }
        }

        private bool Splitit(double splitSize)
        {
        if (splitSize > SmallsplitSize && splitSize < SmallsplitSize * 2.0 )
                 return false;
            return splitSize > SmallsplitSize;
        }
        public void CountLeafNodes(AddCandidate addCandidate)
        {
            if (Lchild != null || Rchild != null)
            {
                if (Lchild != null)
                    Lchild.CountLeafNodes(addCandidate);
                if (Rchild != null)
                    Rchild.CountLeafNodes(addCandidate);
            }
            else
                addCandidate(LeftEdge, TopEdge, RightEdge, BottomEdge);
        }

        public double LeftEdge;
        public double RightEdge;
        public double TopEdge;
        public double BottomEdge;

        public double NodeWidth;
        public double NodeHeight;
        private Orientation SplitOrientation;
        public double SplitLocation;

        public BSPNode Lchild { get; set; }
        public BSPNode Rchild { get; set; }

       static private double SmallsplitSize = .125;

        static private double MsplitRatio = 2f;
        static public Map Map;
        
    }



    